﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using DualityEngine.Business;

namespace DualityClient.MenuComponents
{
    public enum TEXTBOXTYPE
    {
        PASSWORD,
        NONE,
    }

    public class Textbox : GuiControl
    {
        private TEXTBOXTYPE textboxtype;
        public string content;
        private Texture2D hovercontrolbackground;
        public bool isselected = false;
        private MouseState currentmousestate;
        private KeyboardState currentKeyboardState;

        public Textbox(TEXTBOXTYPE textboxtype, Texture2D textbox, Texture2D textboxhovering, Vector2 position)
        {
            this.textboxtype = textboxtype;
            this.position = position;
            this.hovercontrolbackground = textboxhovering;
            this.controlbackground = textbox;
            this.content = "";
        }

        public override void Update(GameTime gameTime)
        {
            MouseState oldmousestate = currentmousestate;
            currentmousestate = Mouse.GetState();

            if (currentmousestate.LeftButton == ButtonState.Released && oldmousestate.LeftButton == ButtonState.Pressed && isHovering())
            {
                this.isselected = true;
            }

            if (isselected)
            {
                Keys[] pressedKeys;
                pressedKeys = currentKeyboardState.GetPressedKeys();
                
                KeyboardState oldKeyboardState = currentKeyboardState;
                currentKeyboardState = Keyboard.GetState(); 

                foreach (Keys key in pressedKeys)
                {
                    if (currentKeyboardState.IsKeyUp(key))
                    {
                        if (key == Keys.Back)
                        {
                            if (content.Length > 0)
                                content = content.Remove(content.Length - 1, 1);
                        }
                        else if (key == Keys.Space)
                            content = content.Insert(content.Length, " ");
                        else
                        {
                            string stringkey = key.ToString();

                            if (currentKeyboardState.IsKeyDown(Keys.LeftShift) || currentKeyboardState.IsKeyDown(Keys.RightShift))
                            {
                                if (stringkey.Length == 1)
                                {
                                    content += key.ToString();
                                }
                                else if (stringkey.Contains("D") && stringkey.Length == 2)
                                {
                                    stringkey = stringkey.Remove(0, 1);

                                    switch (int.Parse(stringkey))
                                    {
                                        case 1:
                                            content += "!";
                                            break;
                                        case 2:
                                            content += "@";
                                            break;
                                        case 3:
                                            content += "#";
                                            break;
                                        case 4:
                                            content += "$";
                                            break;
                                        case 5:
                                            content += "%";
                                            break;
                                        case 6:
                                            content += "^";
                                            break;
                                        case 7:
                                            content += "&";
                                            break;
                                        case 8:
                                            content += "*";
                                            break;
                                        case 9:
                                            content += "(";
                                            break;
                                        case 0:
                                            content += ")";
                                            break;

                                    }
                                }
                                else
                                {
                                    if (stringkey == "OemMinus")
                                    {
                                        content += "_";
                                    }
                                    else if (stringkey == "OemOpenBrackets")
                                    {
                                        content += "{";
                                    }
                                    else if (stringkey == "OemCloseBrackets")
                                    {
                                        content += "}";
                                    }
                                }
                            }
                            else
                            {
                                if (stringkey.Length == 1)
                                {
                                    content += key.ToString().ToLower();
                                }
                                else if (stringkey.Contains("D") && stringkey.Length == 2)
                                {
                                    stringkey = stringkey.Remove(0, 1);
                                    content += stringkey;
                                }
                                else
                                {
                                    if (stringkey == "OemMinus")
                                    {
                                        content += "-";
                                    }
                                    else if (stringkey == "OemOpenBrackets")
                                    {
                                        content += "[";
                                    }
                                    else if (stringkey == "OemCloseBrackets")
                                    {
                                        content += "]";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (isselected)
            {
                ResourceManager.Instance.spriteBatch.Draw(hovercontrolbackground, position, Color.White);
            }
            else
            {
                ResourceManager.Instance.spriteBatch.Draw(controlbackground, position, Color.White);
            }

            string drawstring = content;
            if (textboxtype == TEXTBOXTYPE.PASSWORD)
            {
                string s = "";
                for (int i = 0; i < content.Length; i++)
                {
                    s += "*";
                }
                drawstring = s;
            }            
            Color textcolor = Color.Magenta;
            if (isselected)
                textcolor = Color.Black;
            ResourceManager.Instance.spriteBatch.DrawString(ResourceManager.Instance.kootenay, drawstring, new Vector2(position.X + 10, position.Y + 7), textcolor);
        }
    }
}
